%%%-------------------------------------------------------------------
%%% @author Rain
%%% @doc 网页后台相关
%%% @end
%%%-------------------------------------------------------------------
-module(web).
-author("Rain").

%% API
-export([start_http_service/0]).

%% @doc 启动http服务
%% Middlewares = maps:get(middlewares, Opts, [cowboy_router, cowboy_handler]) % 默认中间件
start_http_service() ->
    Routers = http_routers(),
    Dispatch = cowboy_router:compile(Routers),
    WebPort = config:get(web_port),
    TransOpts = [{port, WebPort}],
    Middlewares = [cowboy_router, web_middleware, cowboy_handler], % 新增一个中间件处理一些http选项
    ProtoOpts = #{env => #{dispatch => Dispatch}, middlewares => Middlewares},
    {ok, _} = cowboy:start_clear(game_http_listener, TransOpts, ProtoOpts),
    ok.

%% @doc http路由
http_routers() ->
    [{'_', http_assets_paths() ++ http_api_paths()}].

%% @doc Api接口，默认就是api接口
http_api_paths() ->
    [{'_', http_handler, []}].
%%    [{"/api/[...]", http_handler, []}].

%% @doc 资源文件
http_assets_paths() ->
    [{"/assets/[...]", cowboy_static, {dir, "assets"}}].